package com.home.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.home.school.bean.TSubject;
import com.home.school.bean.TUser;
import com.home.school.constant.OrderStatus;
import com.home.school.service.TSubjectService;
import com.home.school.service.TUserService;
import com.home.school.vo.*;
import com.home.school.model.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.home.school.mapper.TOrderMapper;
import com.home.school.bean.TOrder;
import com.home.school.service.TOrderService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService{

    @Resource
    private TUserService tUserService;
    @Resource
    private TSubjectService tSubjectService;

    @Override
    public R userList(OrderList userListVO) {
        Page<TOrder> page = new Page<>(userListVO.getPageNum(), userListVO.getPageSize());
        return R.ok().data(baseMapper.selectPage(page, queryWrapper(userListVO)));
    }

    @Override
    public R teaList(OrderList userListVO) {
        Page<TOrder> page = new Page<>(userListVO.getPageNum(), userListVO.getPageSize());
        IPage<TOrder> tOrderIPage = baseMapper.selectPage(page, queryWrapper(userListVO));
        List<TOrder> records = tOrderIPage.getRecords();
        for (TOrder record : records) {
            String stuId = record.getStuId();
            Integer subjectId = record.getSubjectId();
            TSubject tSubject = tSubjectService.getById(subjectId);
            record.setSubjectType(tSubject.getType());
            TUser user = tUserService.getById(stuId);
            if(Objects.nonNull(user)){
                record.setUserName(user.getUsername());
            }
            record.setSubjectTitle(tSubject.getTitle());
        }
        return R.ok().data(tOrderIPage);
    }

    @Override
    public R orderList(OrderList userListVO) {
        Page<TOrder> page = new Page<>(userListVO.getPageNum(), userListVO.getPageSize());
        IPage<TOrder> tOrderIPage = baseMapper.selectPage(page, queryWrapper(userListVO));
        List<TOrder> records = tOrderIPage.getRecords();
        for (TOrder record : records) {
            String stuId = record.getStuId();
            Integer subjectId = record.getSubjectId();
            TSubject tSubject = tSubjectService.getById(subjectId);
            record.setSubjectType(tSubject.getType());
            TUser user = tUserService.getById(stuId);
            if(Objects.nonNull(user)){
                record.setUserName(user.getUsername());
            }
            record.setSubjectTitle(tSubject.getTitle());
        }
        return R.ok().data(tOrderIPage);
    }

    @Override
    public R confirm(OrderConfirmVO orderConfirmVO) {
        String orderId = orderConfirmVO.getOrderId();
        TOrder tOrder = baseMapper.selectById(orderId);
        tOrder.setStatus(OrderStatus.DEAL.getStatus());
        baseMapper.updateById(tOrder);
        return R.ok();
    }

    @Override
    public R cancel(OrderConfirmVO orderConfirmVO) {
        String orderId = orderConfirmVO.getOrderId();
        TOrder tOrder = baseMapper.selectById(orderId);
        tOrder.setStatus(OrderStatus.CANCEL.getStatus());
        baseMapper.updateById(tOrder);
        return R.ok();
    }

    @Override
    public R over(OrderConfirmVO orderConfirmVO) {
        String orderId = orderConfirmVO.getOrderId();
        TOrder tOrder = baseMapper.selectById(orderId);
        tOrder.setStatus(OrderStatus.OVER.getStatus());
        baseMapper.updateById(tOrder);
        return R.ok();
    }

    @Override
    public R reject(OrderConfirmVO orderConfirmVO) {
        String orderId = orderConfirmVO.getOrderId();
        TOrder tOrder = baseMapper.selectById(orderId);
        tOrder.setStatus(OrderStatus.REJECT.getStatus());
        tOrder.setRejectMsg(orderConfirmVO.getRejectMsg());
        baseMapper.updateById(tOrder);
        return R.ok();
    }

    public QueryWrapper<TOrder> queryWrapper(OrderList tOrder) {
        QueryWrapper<TOrder> tUserQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tOrder.getAddress())){
            tUserQueryWrapper.eq("address", tOrder.getAddress());
        }
        if(Objects.nonNull(tOrder.getStatus())){
            tUserQueryWrapper.eq("status", tOrder.getStatus());
        }
        if(Objects.nonNull(tOrder.getTeaId())){
            tUserQueryWrapper.eq("tea_id", tOrder.getTeaId());
        }
        if(Objects.nonNull(tOrder.getStuId())){
            tUserQueryWrapper.eq("stu_id", tOrder.getStuId());
        }
        if(Objects.nonNull(tOrder.getSubjectId())){
            tUserQueryWrapper.eq("subject_id", tOrder.getSubjectId());
        }
        return tUserQueryWrapper;
    }

}
